var common = require('../../../utils/common.js')
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    daan: "请选择答案",
    testid: '',
    like: false,
    topic: "",
    testid: "",
    collectid: "",
    examid: "",
    parid: "",
    shitiided: "",
    iscorrect:""


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      remarktype: options.remarktype,
      testid: options.testid,
      examtype: options.examtype,
      subscribing: options.subscribing,
      iscorrect: options.iscorrect
    })
    wx.setNavigationBarTitle({
      title: options.title//页面标题为路由参数
    })
    console.log(this.data.remarktype);
    this.dataCenter();
  },
  //2.取出个人信息，机构信息，题目信息
  dataCenter: function () {
    var orgid = common.getStorageSync("orgid")
    var userid = common.getStorageSync("userid")
    this.setData({
      orgid: orgid,
      userid: userid,
    });
    this.paridpost();
  },
  paridpost: function () {
    // 8.13.1 获取试题内容（已测试）（以下接口只有在该题目是自动批改时才要调用）
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "id": this.data.testid }
    businessLogic.TestContent(params, (res) => {
      console.log(res);
      if (res.substate == 1) {
        this.setData({
          show: 1
        });
      } else {
        this.setData({
          show: 0
        });
      }
      // this.dati(res);//把题目信息交给答题组件渲染
      this.subject.dingzhentimu(res, this.data.remarktype, this.data.testid);//把所有的题目交给subject组件,并去获取第一题
      this.setData({
        topic: res,
      });
      //   this.findInfoTempListByClassid(testid);
      this.chacollect(this.data.testid);
    })

  },
  //将题目信息渲染成 大题和小题
  //查询是否收藏
  chacollect: function () {
    let params = { userid: this.data.userid, orgid: this.data.orgid, searchField: "peopleid,testid", searchValue: "" + this.data.userid + "," + this.data.testid + "" }
    businessLogic.CollectionOfPapers(params, (res) => {
      console.log(res);
      // 0是没有收藏
      // 1是收藏
      if (res == undefined) {

      } else {
        if (res.iscollect == 1) {
          this.setData({
            like: true,
            collectid: res.id
          })
        } else {
          this.setData({
            like: false,
            collectid: res.id

          })
        }
      }
    });
  },
  collect: function () {
    this.setData({
      like: !this.data.like
    })

    if (this.data.like == true) {
      console.log(this.data.like);
      console.log(this.data.testid);

      // 1收藏
      // 0取消收藏
      let InfoJson = ' {"orgid":"' + this.data.orgid + '", "userid":"' + this.data.userid + '", "peopleid": "' + this.data.userid + '", "testid":" ' + this.data.testid + '", "iscollect": "1", "id": "' + this.data.collectid + '"}';
      let params = {
        "InfoJson": InfoJson, "orgid": this.data.orgid, "userid": this.data.userid

      }
      businessLogic.CancelTheCollection(params, (res) => {
        console.log(res);


      });




    } else {
      // console.log(this.data.like);
      // console.log(this.data.testid);
      let InfoJson = ' {"orgid":"' + this.data.orgid + '", "userid":"' + this.data.userid + '", "peopleid": "' + this.data.userid + '", "testid":" ' + this.data.testid + '", "iscollect": "0", "id": "' + this.data.collectid + '"}';

      let params = {
        "InfoJson": InfoJson, "orgid": this.data.orgid, "userid": this.data.userid
      }
      businessLogic.CancelTheCollection(params, (res) => {
        console.log(res);



      });

    }


  },
  // sure: function () {

  // wx.navigateBack({
  //   delta: 1
  // })
  // },
  sure: function () {
    //获取答案
    let sureAnswer = this.subject.sure();

    let daan = sureAnswer["mydata"];//答案
    let score = sureAnswer["rubric"].score;//这道题的总分
    let knowledgepoints = sureAnswer["rubric"].knowledgepoints;//测量主题的id

    var typeGail = sureAnswer["rubric"].typecode;//获取填空的题目的类型
    for (let i = 0; i < typeGail.length; i++) {
      if (typeGail[i] == "填空题_键盘输入类型") {
        typeGail[i] = "1";
      } else if (typecode == "填空题_语言输入类型") {
        typeGail[i] = "3";
      } else if (typecode == "填空题_手绘输入类型") {
        typeGail[i] = "2";
      }
    }

    var typecode = sureAnswer["rubric"].typecode;//获取题目的类型
    if (typecode == "填空题_键盘输入类型") {
      typecode = "2";
    } else if (typecode == "填空题_语言输入类型") {
      typecode = "2";
    } else if (typecode == "填空题_手绘输入类型") {
      typecode = "2";
    } else if (typecode == "单选题") {
      typecode = "0";
    } else if (typecode == "多选题") {
      typecode = "1";
    } else {
      typecode = "2";
    }

    let newdaan = [];
    let weidaan = [];
    let newdaan2 = [];
    let weidaan2 = [];

    if (typecode == "0" || typecode == "1") {
      weidaan = daan;
      weidaan2 = daan;
    } else {
      let newdaansuan = {};
      let newdaansuan2 = {};
      for (var a = 0; a < daan.length; a++) {
        let c = "" + a + "";
        newdaansuan = '{' + "\\" + '"data' + "\\" + '":' + "\\" + '"' + daan[a] + '' + "\\" + '",' + "\\" + '"type' + "\\" + '":' + typeGail[a] + ',' + "\\" + '"order' + "\\" + '":' + c + '},';
        newdaan = newdaan + newdaansuan
        newdaansuan2 = '{"data":"' + daan[a] + '","type":' + typeGail[a] + ',"order":' + c + '},';
        newdaan2 = newdaan2 + newdaansuan2
      }
      newdaan = newdaan.substring(0, newdaan.length - 1);
      weidaan = newdaan + weidaan;
      weidaan = "[" + weidaan + "]";

      newdaan2 = newdaan2.substring(0, newdaan2.length - 1);
      weidaan2 = newdaan2 + weidaan2;
      weidaan2 = "[" + weidaan2 + "]";
    }
    if (weidaan == "" || weidaan == "1" || weidaan2 == "" || weidaan2 == "1") {
      common.showToast("请输入答案", "none");
    } else {
      // console.log("weidaan2", weidaan2);
      // 8.3.2.1  第一次点击确认后查询人员试题表（）
      let params = { "peopleid": this.data.userid, "orgid": this.data.orgid, "examid": "", "testid": this.data.testid, "stuanswer": weidaan2, "score": 0, "modelname": "人员试题记录系统模型", "typecode": typecode }
      businessLogic.AddExamination(params, (res) => {
        common.showLoading("加载中", "loading");
        console.log("8.3.2.1", res);




        this.setData({
          queding_id: res.result.id,
          queding_haspg: res.infos[0].haspg,
          queding_point: res.infos[0].point,
          queding_statemark: res.infos[0].statemark,
        })

        if (res.result.flag == 1) {
          var statemarktype = "";
          if (res.infos[0].statemark == null) {
            statemarktype = 0;
          } else {
            statemarktype = res.infos[0].statemark;
          }

          // 8.3.2.2  点击确认后更新人员试题表
          let InfoJson = '{"id":"' + res.result.id + '","type":"' + statemarktype + '"}';
          let params = { "InfoJson": InfoJson, "userid": this.data.userid, "orgid": this.data.orgid };
          businessLogic.UpdateStaffTest(params, (resa) => {
            common.showLoading("加载中", "loading");
            common.hideLoading();
            console.log("8.3.2.2 ", resa);
            if (resa.flag == 1) {
              console.log("shitiided", this.data.shitiided)
              // this.subject.quesure(, , , , , , , , );
              // quesure: function (, , , , , , , , )
              let classid = res.result.id;
              let statemark = statemarktype;
              let parid = this.data.parid;
              let examid = "";
              let daan = weidaan;
              let point = res.infos[0].point;
              let testid = this.data.testid;
              let shitiided = this.data.shitiided;
              let iscorrect = this.data.iscorrect;//1 是订正的题 0表示考试题目 2是收藏练习

              if (score == "") {
                score = 0;
              }
              if (common.getStorageSync("role") == "学生") {
                //8.3.2.3 点击确认后更新人员试题记录表（）
                let kaisi = common.getStorageSync("kaisi");
                let timestamp = (new Date()).valueOf();
                let dotime = timestamp - kaisi;

                var timestamp = Math.round(new Date().getTime() / 1000).toString();
                let InfoJson = '{"orgid":"' + this.data.orgid + '","userid":"' + this.data.userid + '","classid":"' + classid + '","id":"' + shitiided + '","addtime":"' + timestamp + '","haspg":"0","hastj":"0","statemark":"' + statemark + '","dotime":"' + dotime + '","paperid":"' + parid + '","examid":"' + examid + '","peopleid":"' + this.data.userid + '","answer":"' + sureAnswer["rubric"].answer + '","iscorrect":"' + iscorrect + '","newstext":"' + daan + '","substates":"' + sureAnswer["rubric"].substate + '","point":' + point + ',"score":"0","testid":"' + testid + '","parentid":""}';
                let params = { "InfoJson": InfoJson, "userid": this.data.userid, "orgid": this.data.orgid, classid: classid };
                businessLogic.UpdateStaffTestnew(params, (resaa) => {
                  common.showLoading("加载中", "loading");
                  console.log("8.3.2.3", resaa);
                  common.showToast("已确定", "none");
                  if (resaa.flag == 1) {

                    // ==========================薄弱知识点的接口
                    // haspg  1 批改 0 属于不批改
                    // 学生 1调用   老师 0 调用

                    if (res.infos[0].haspg == 1 && common.getStorageSync("role") == "学生") {
                      // 8.10.2 查询该人是否有该薄弱知识点的接口（已测试）
                      let chaxun = { "searchField": "peopleid, knowledgepoints", "searchValue": "" + this.data.userid + "," + knowledgepoints + "", "orgid": this.data.orgid, userid: this.data.userid }
                      businessLogic.QueryWhethernew(chaxun, (fres) => {
                        console.log("8.10.2", fres);
                        if (fres.result.flag == 1) {
                          let id = "";
                          let acount = "1";//题数               
                          let wcount = "0";//错题数

                          if (fres.infos == "" || fres.infos == undefined) {
                            id = "";
                            acount = 1;
                            if (res.infos[0].statemark == 0) {
                              wcount = 1;
                            } else {
                              wcount = 0
                            }
                          } else {
                            id = fres.infos[0].id;
                            acount = fres.infos[0].acount + 1;
                            let newc = fres.infos[0].wcount
                            // console.log("cg");
                            // statemark 0 错
                            if (res.infos[0].statemark == 0) {
                              wcount = newc + 1;
                            } else {
                              wcount = newc
                            }
                          }
                          let jilu = '{"orgid":"' + this.data.orgid + '","userid":"' + this.data.userid + '","classid":"e956a1e0-52a2-4a71-a5c6-3ffc6852877c","id":"' + id + '","peopleid":"' + this.data.userid + '","knowledgepoints":"' + knowledgepoints + '","acount":"' + acount + '","wcount":"' + wcount + '"}';
                          let jilus = { "InfoJson": "" + jilu + "", "userid": "" + this.data.userid + "", "orgid": "" + this.data.orgid + "" }
                          businessLogic.InterfaceWeakKnowledgenew(jilus, (cres) => {
                            console.log(cres);
                            //跳转
                            wx.navigateBack({
                              delta: 2
                            })

                          });
                        }
                      });
                    }
                    else if (res.infos[0].haspg == 0 && this.data.role == "老师") {
                      // 8.10.2 查询该人是否有该薄弱知识点的接口（已测试）
                      let chaxun = { "searchField": "peopleid, knowledgepoints", "searchValue": "" + this.data.userid + "," + knowledgepoints + "", "orgid": this.data.orgid, userid: this.data.userid }
                      businessLogic.QueryWhethernew(chaxun, (fres) => {
                        console.log("8.10.2", fres);
                        if (fres.result.flag == 1) {
                          let id = "";
                          let acount = "1";//题数               
                          let wcount = "0";//错题数

                          if (fres.infos == "" || fres.infos == undefined) {
                            id = "";
                            acount = 1;
                            if (res.infos[0].statemark == 0) {
                              wcount = 1;
                            } else {
                              wcount = 0
                            }
                          } else {
                            id = fres.infos[0].id;
                            acount = fres.infos[0].acount + 1;
                            let newc = fres.infos[0].wcount
                            console.log("cg");
                            // statemark 0 错
                            if (res.infos[0].statemark == 0) {
                              wcount = newc + 1;
                            } else {
                              wcount = newc
                            }
                          }
                          let jilu = '{"orgid":"' + this.data.orgid + '","userid":"' + this.data.userid + '","classid":"e956a1e0-52a2-4a71-a5c6-3ffc6852877c","id":"' + id + '","peopleid":"' + this.data.userid + '","knowledgepoints":"' + knowledgepoints + '","acount":"' + acount + '","wcount":"' + wcount + '"}';
                          let jilus = { "InfoJson": "" + jilu + "", "userid": "" + this.data.userid + "", "orgid": "" + this.data.orgid + "" }
                          businessLogic.InterfaceWeakKnowledgenew(jilus, (cres) => {
                            console.log(cres);
                          });
                        }
                      });
                    }

                    // ==========================
                  } else {

                  }

                });
              } else if (this.data.role == "老师") {
                // 这边是批改
                // 6.8.3.3 点击确认后更新人员试题记录表（）
                let py = this.subject.getdepy();

                let defen = py[0];//总分
                let pinjia = py[1];//评价
                let point = py[2];//得分
                let statemark = "";//错误

                if (defen == point) {
                  statemark = 0;
                } else {
                  statemark = 1;
                }

                let queding = '{"orgid":"' + this.data.orgid + '","userid":"' + this.data.userid + '","classid":"' + res.result.id + '","id":"' + shitiided + '","point":"' + point + '","haspg":"1","hastj":"0","statemark":"' + statemark + '","say":"' + pinjia + '","pgmemberid":"' + this.data.userid + '"}';
                let quezhi = { "InfoJson": queding, "userid": "" + this.data.userid + "", classid: "" + res.result.id + "", orgid: "" + this.data.orgid + "" }

                // 6.8.3.3 点击确认后更新人员试题记录表
                businessLogic.DetermineTheUpdate(quezhi, (cres) => {
                  common.showLoading("加载中", "loading");
                  console.log(cres);
                  common.hideLoading();
                  common.showToast("已确定", "none");
                });



                // 先不做判断
                // this.findInfoTempListByClassid(testid);

              }


            }
          });
        }
      });

    }

  }, //确定提交
  // sure: function () {
  //   var a = this.subject.sure();
  //   // console.log(a);
  //   var daan = a['mydata'];
  //   var score = "";
  //   var typeGail = a['rubric'].typecode;
  //   for (let i = 0; i < typeGail.length; i++) {
  //     if (typeGail[i] == "填空题_键盘输入类型") {
  //       typeGail[i] = "1";
  //     } else if (typecode == "填空题_语言输入类型") {
  //       typeGail[i] = "3";
  //     } else if (typecode == "填空题_手绘输入类型") {
  //       typeGail[i] = "2";
  //     }
  //   }

  //   var typecode = a['rubric'].typecode;
  //   if (typecode == "填空题_键盘输入类型") {
  //     typecode = "2";
  //   } else if (typecode == "填空题_语言输入类型") {
  //     typecode = "2";
  //   } else if (typecode == "填空题_手绘输入类型") {
  //     typecode = "2";
  //   } else if (typecode == "单选题") {
  //     typecode = "0";
  //   } else if (typecode == "多选题") {
  //     typecode = "1";
  //   } else {
  //     typecode = "2";
  //   }

  //   let newdaan = [];
  //   let weidaan = [];
  //   let newdaan2 = [];
  //   let weidaan2 = [];

  //   if (typecode == "0" || typecode == "1") {
  //     weidaan = daan;
  //     weidaan2 = daan;
  //   } else {
  //     let newdaansuan = {};
  //     let newdaansuan2 = {};
  //     for (var a = 0; a < daan.length; a++) {
  //       let c = "" + a + "";
  //       newdaansuan = '{' + "\\" + '"data' + "\\" + '":' + "\\" + '"' + daan[a] + '' + "\\" + '",' + "\\" + '"type' + "\\" + '":' + typeGail[a] + ',' + "\\" + '"order' + "\\" + '":' + c + '},';
  //       newdaan = newdaan + newdaansuan
  //       newdaansuan2 = '{"data":"' + daan[a] + '","type":' + typeGail[a] + ',"order":' + c + '},';
  //       newdaan2 = newdaan2 + newdaansuan2
  //     }
  //     newdaan = newdaan.substring(0, newdaan.length - 1);
  //     weidaan = newdaan + weidaan;
  //     weidaan = "[" + weidaan + "]";

  //     newdaan2 = newdaan2.substring(0, newdaan2.length - 1);
  //     weidaan2 = newdaan2 + weidaan2;
  //     weidaan2 = "[" + weidaan2 + "]";
  //   }
  //   if (weidaan == "" || weidaan == "1" || weidaan2 == "" || weidaan2 == "1") {
  //     common.showToast("请输入答案", "none");
  //   } else {
  //     console.log(daan);
  //     // 8.3.2.1  点击确认后查询人员试题表（）
  //     let params = { "peopleid": this.data.userid, "orgid": this.data.orgid, "examid": this.data.examid, "testid": this.data.testid, "stuanswer": weidaan2, "score": score, "modelname": "人员试题记录系统模型", "typecode": typecode }
  //     businessLogic.AddExamination(params, (res) => {
  //       console.log(res);
  //       this.setData({
  //         queding_id: res.result.id,
  //         queding_haspg: res.infos[0].haspg,
  //         queding_point: res.infos[0].point,
  //         queding_statemark: res.infos[0].statemark,
  //       })

  //       if (res.result.flag == 1) {
  //         var statemarktype = "";
  //         if (res.infos[0].statemark == null) {
  //           statemarktype = 0;
  //         } else {
  //           statemarktype = res.infos[0].statemark;
  //         }
  //         // 8.3.2.2  点击确认后更新人员试题表
  //         let InfoJson = '{"id":"' + res.result.id + '","type":"' + statemarktype + '"}';
  //         let params = { "InfoJson": InfoJson, "userid": this.data.userid, "orgid": this.data.orgid };
  //         businessLogic.UpdateStaffTest(params, (resa) => {
  //           console.log(resa);
  //           if (resa.flag == 1) {
  //             var zhuan = this.subject.quesure(res.result.id, statemarktype, this.data.parid, this.data.examid, weidaan, res.infos[0].point, this.data.testid, this.data.shitiided, 1);
  //             console.log(zhuan);
  //             common.showToast("提交成功","success")
  //             wx.navigateBack({
  //               delta: 1
  //             })

  //           }
  //         });
  //       }
  //     });

  //   }

  // },
  // 这边要注册组件
  onReady: function () {
    //获得subject组件
    this.subject = this.selectComponent("#subject");
  },

})